package com.quanyan.user.web;

import com.quanyan.BaseRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.common.user.AppUserCacheInfo;
import com.quanyan.common.utils.DateUtils;
import com.quanyan.user.model.vo.request.*;
import com.quanyan.user.model.vo.response.RespUserBindingInfo;
import com.quanyan.user.model.vo.response.RespUserCookiesName;
import com.quanyan.user.model.vo.response.RespUserLogin;
import com.quanyan.user.model.vo.response.RespUserSecurity;
import com.quanyan.user.service.UserLoginService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by zhang on 16/11/11.
 */
@RestController
@RequestMapping(value = "/api/user/login",method = RequestMethod.POST)
public class UserLoginController {

    private Logger logger = LoggerFactory.getLogger(UserLoginController.class);
    @Autowired
    private UserLoginService userLoginService;

    /**
     * 获取验证码
     * @param map 验证码请求参数
     * @return APIResponse
     */
    @RequestMapping(value = "validCode")
    public APIResponse validCode(@RequestBody Map map){
        String phone;
        if (map.containsKey("phone")) {
            phone = (String) map.get("phone");
        } else {
            return APIResponse.returnFail("缺少必要参数");
        }
        logger.info("手机号为【{}】的用户在【{}】发送了验证码",phone, DateUtils.getStandardCurrentTime());
        return userLoginService.getValidCode(phone);
    }

    /**
     * 初始化用户cookies
     * @param appUserCacheInfo
     * @return
     */
    @RequestMapping(value = "appInit")
    public APIResponse<RespUserCookiesName> appInit(@RequestBody AppUserCacheInfo appUserCacheInfo){
        RespUserCookiesName userCookiesName = userLoginService.appInit(appUserCacheInfo);
        return APIResponse.returnSuccess(userCookiesName);
    }

    /**
     * 手机号码验证
     * @param reqUserSecurity
     * @return
     */
    @RequestMapping(value = "validateMobile")
    public APIResponse<RespUserSecurity> validateMobile(@RequestBody ReqUserSecurity reqUserSecurity){
        return this.userLoginService.validateMobile(reqUserSecurity);
    }

    /**
     * 用户验证码注册
     * @param reqUserRegister
     * @return
     */
    @RequestMapping(value = "verifyReg")
    public APIResponse<RespUserLogin> verifyReg(@RequestBody ReqUserRegister reqUserRegister,HttpServletRequest request){
        reqUserRegister.setUserCookiesName((String) request.getAttribute("userCookiesName"));
        AppUserCacheInfo cookies = (AppUserCacheInfo)request.getAttribute("cookies");
        if (request.getAttribute("appChannel") != null) {
            reqUserRegister.setAppChannel((String) request.getAttribute("appChannel"));
        }
        if (cookies != null) {
            reqUserRegister.setDevType(cookies.getDev());
        }
        return userLoginService.verifyRegister(reqUserRegister);
    }

    /**
     * 用户登陆：共5种方式
     * 密码、验证码、微信、微博、qq
     * @return
     */
    @RequestMapping(value = "login")
    public APIResponse login(@RequestBody ReqUserLogin reqUserLogin,HttpServletRequest request){
        reqUserLogin.setUserCookiesName((String) request.getAttribute("userCookiesName"));
        if (request.getAttribute("appChannel") != null) {
            reqUserLogin.setAppChannel((String) request.getAttribute("appChannel"));
        }
        AppUserCacheInfo cookies = (AppUserCacheInfo)request.getAttribute("cookies");
        if (cookies != null) {
            reqUserLogin.setDevType(cookies.getDev());
        }
        return userLoginService.login(reqUserLogin);
    }

    /**
     * 修改密码
     * @param reqUserChangePassword
     * @return
     */
    @RequestMapping(value = "changePassword")
    public APIResponse changePassword(@RequestBody ReqUserChangePassword reqUserChangePassword){
        return userLoginService.changePassword(reqUserChangePassword);
    }

    /**
     * 密码验证
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "verifyPassword")
    public APIResponse verifyPassword(@RequestBody Map map,HttpServletRequest request){
        int uid = (int) request.getAttribute("uid");
        String password ;
        if (map.containsKey("password")) {
            password = (String) map.get("password");
        } else {
            return APIResponse.returnFail("缺少必要参数");
        }
        return userLoginService.verifyPassword(uid, password);
    }

    /**
     * 获取用户信息
     * @param reqGetUser
     * @return
     */
    @RequestMapping(value = "getUser")
    public APIResponse<RespUserCookiesName> getUser(@RequestBody ReqGetUser reqGetUser){
        return userLoginService.getUser(reqGetUser);
    }

    /**
     * 绑定用户微信账号
     * @param reqUserBinding
     * @return
     */
    @RequestMapping(value = "bindingWxAccount")
    public APIResponse<RespUserCookiesName> bindingWxAccount(@RequestBody ReqUserBinding reqUserBinding){
        return userLoginService.userBinding(reqUserBinding);
    }

    /**
     * 验证、绑定、解绑第三方账号
     * @param reqUserLogin
     * @return
     */
    @RequestMapping(value = "thirdAccount")
    public APIResponse validBindingThirdAccount(@RequestBody ReqUserLogin reqUserLogin,HttpServletRequest request){
        int operateType = reqUserLogin.getOperateType();
        if (operateType == 3 || operateType == 4) {
            if (request.getAttribute("uid") != null && (int)request.getAttribute("uid") != 0) {
                reqUserLogin.setUid((int) request.getAttribute("uid"));
            } else {
                return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
            }
        }
        if (request.getAttribute("appChannel") != null) {
            reqUserLogin.setAppChannel((String) request.getAttribute("appChannel"));
        }
        AppUserCacheInfo cookies = (AppUserCacheInfo)request.getAttribute("cookies");
        if (cookies != null) {
            reqUserLogin.setDevType(cookies.getDev());
        }
        reqUserLogin.setUserCookiesName((String)request.getAttribute("userCookiesName"));
        return userLoginService.validBindingThirdAccount(reqUserLogin);
    }

    /**
     * 用户cookies验证
     * @param request
     * @return
     */
    @RequestMapping(value = "validateCookies")
    public APIResponse validateCookies(HttpServletRequest request){
        if (request.getAttribute("uid") != null) {
            return userLoginService.validateCookies((int)request.getAttribute("uid"));
        } else {
            return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
        }

    }

    /**
     * 用户账号绑定列表
     * @param request
     * @return
     */
    @RequestMapping(value = "bindingList")
    public APIResponse<RespUserBindingInfo> bindingList(HttpServletRequest request,@RequestBody BaseRequest baseRequest){
        if (request.getAttribute("uid") != null && (int) request.getAttribute("uid") != 0) {
            int uid = (int) request.getAttribute("uid");
            int larkAppId = baseRequest.getLarkAppId();
            return  userLoginService.bindingList(uid,larkAppId);
        } else {
            return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
        }
    }

    /**
     * 退出登录
     * @param map
     * @param request
     * @return
     */
    @RequestMapping(value = "logout")
    public APIResponse<RespUserCookiesName> logout(@RequestBody Map map,HttpServletRequest request){
        if (!map.containsKey("userCookiesName")) {
            return APIResponse.returnFail("缺少必要参数!!");
        }
        if (request.getAttribute("uid") != null && (int) request.getAttribute("uid") != 0) {
            map.put("uid",request.getAttribute("uid"));
        } else {
            return APIResponse.returnFail(ApiResponseEnum.NEED_USER_LOGIN);
        }

        return userLoginService.logout(map);
    }

    /**
     * 达人B端登陆
     * @param reqDarenLogin
     * @param request
     * @return
     */
    @RequestMapping(value = "darenLogin")
    public APIResponse<RespUserLogin> darenLogin(@RequestBody ReqDarenLogin reqDarenLogin,HttpServletRequest request){
        reqDarenLogin.setUserCookiesName((String) request.getAttribute("userCookiesName"));
        return userLoginService.darenLogin(reqDarenLogin);
    }



}
